package com.library.management.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.library.management.common.PageResult;
import com.library.management.entity.Book;
import com.library.management.mapper.BookMapper;
import com.library.management.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 图书服务实现类
 */
@Service
public class BookServiceImpl implements BookService {
    @Autowired
    private BookMapper bookMapper;

    @Override
    public int add(Book book) {
        return bookMapper.insert(book);
    }

    @Override
    public int delete(Long id) {
        return bookMapper.deleteById(id);
    }

    @Override
    public int update(Book book) {
        return bookMapper.update(book);
    }

    @Override
    public Book getById(Long id) {
        return bookMapper.selectById(id);
    }

    @Override
    public PageResult<Book> list(Book book, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Book> list = bookMapper.selectList(book);
        PageInfo<Book> pageInfo = new PageInfo<>(list);
        return new PageResult<>(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public int updateStock(Long id, Integer stock) {
        return bookMapper.updateStock(id, stock);
    }
}